package com.swhy

import com.utils._
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import org.codehaus.jackson.map.ObjectMapper

import java.net.URI
import java.util.{Date, UUID}


object ChgeProdSaleRpt {
  def main(args: Array[String]): Unit = {
    if (args.length != 3) {
      println(
        """
          |com.swhy.CompanyInfo
          |params:
          | InputDate    刷数日期
          | OutputPath   输出路径
          | SplitNum     文件生成数量
        """.stripMargin)
      sys.exit()
    }
    val conf = new Configuration()
    val Array(inputDt, outputPath, splitNum) = args
    val sparkConf = new SparkConf().setAppName(this.getClass.getName)
    val sc = new SparkContext(sparkConf)
    val hiveContext = new HiveContext(sc)
    //改下方的sql为需要装换成jason的sql
    val allcompany =
      s"""
      select
      c.prod_code as  prod_code,
      d.prod_name,
      a.asset_acct as capi_acc,
      b.ecif_cd,
      b.cust_no,
      b.cust_name,
      nvl(b.mgr_serv,'') as mgr_cd,
      nvl(b.mgr_dev,'') as mgr_dev,
      nvl(b.mgr_sign,'') as mgr_sign,
      nvl(b.brch_org_level_cd_serv,'')  as brch_org_level_cd,
      nvl(b.brch_org_level_cd_dev,'')  as brch_org_level_cd_dev,
      nvl(b.brch_org_level_cd_sign,'')  as brch_org_level_cd_sign,
      nvl(b.brch_org_cd_serv,'')  as brch_org_cd,
      nvl(b.brch_org_cd_dev,'')  as brch_org_cd_dev,
      nvl(b.brch_org_cd_sign,'')  as brch_org_cd_sign,
      b.latest_risk_grad,
      b.cust_grad,
      b.cust_type,
      round(nvl(b.cap_bal,0)) as  cap_bal,
      round(nvl(a.total_asset,0)) as prod1_total_asset,
      round(nvl(h.xjlzc,0)) as prod1_cap_bal,
      nvl(h.xjlzc_mx,'[]') as prod1_cap_bal_detail,
      round(nvl(a.dz_asset,0)) as prod1_dz_asset,
      round(nvl(a.hb_asset,0))as prod1_hb_asset,
      round(nvl(a.fund_asset_rate,0)) as prod1_fund_asset_rate,
      nvl(a.pos_hb_flag,'0')as prod1_pos_hb_flag,
      nvl(a.pos_dz_flag,'0') as prod1_pos_dz_flag,
      nvl(a.pos_bzj_flag,'0') as prod1_pos_bzj_flag,
      max(case when c.bq_flag='dj'  then '1'  else '0'  end) as prod1_dj_flag,
      max(case when c.bq_flag='pos'  then '1'  else '0'  end)  as prod1_pos_flag,
      max(case when c.bq_flag='buy'  then '1'  else '0'  end)  as prod1_buy_flag,
      max(case when f.asset_acct is  null then '0' else '1' end ) as prod1_buy_gp,
      max(case when g.asset_acct is null then '0' else '1' end ) as prod1_is_buy
      from app_data.app_rpt_fund_latent_acct_info_d_z  a
      left join app_data.app_rpt_fund_latent_acct_flag_d_z c
      on a.asset_acct =c.asset_acct
      join pdm_data.pdm_t02_prod_q d
      on concat('F',c.prod_code) =d.prod_id
      left join  app_data.app_dao_acct_info_d_q  b
      on  a.asset_acct =  b.capi_acc
      left join (select asset_acct from  sum_data.sum_t98_on_board_match_dtl_z a join pdm_data.pdm_t02_prod_q b on a.prod_id =b.prod_id where trim(prod_type_cd)  in('S00','S10','S30','S20','S22','S01','S11','S31','S21','S36','S21','S32','S33','S34','S35') and a.biz_date>=cast(regexp_replace(add_months(from_unixtime(unix_timestamp('${inputDt}','yyyyMMdd' ),'yyyy-MM-dd'),-12),'-','') as int) and b.biz_date=${inputDt})f
      on a.asset_acct =f.asset_acct
      left join app_data.app_daop_recmd_aft_purc_z g
      on  a.asset_acct =g.asset_acct
      and c.prod_code= g.prod_id
      left join app_data.xjlzc_daop_temp  h
      on a.asset_acct=h.asset_acct
      where b.biz_date=${inputDt}
      and a.biz_date=${inputDt}
      and d.biz_date=${inputDt}
      and c.biz_date=${inputDt}
      group by  c.prod_code,d.prod_name,a.asset_acct,b.ecif_cd,
                b.cust_no,b.cust_name,b.mgr_serv,b.mgr_dev,b.mgr_sign,
                b.brch_org_level_cd_serv,b.brch_org_level_cd_dev,
                b.brch_org_level_cd_sign,b.brch_org_cd_serv,
                b.brch_org_cd_dev,b.brch_org_cd_sign,b.latest_risk_grad,
                b.cust_grad,b.cust_type,b.cap_bal,a.total_asset,
                a.cap_bal,a.dz_asset,a.hb_asset,a.fund_asset_rate,a.pos_hb_flag,
                a.pos_dz_flag,a.pos_bzj_flag,h.xjlzc,h.xjlzc_mx
      """.stripMargin
    print(allcompany)
    val frame  = hiveContext.sql(allcompany)
    val result = frame.rdd.map(x => {
      var cominfo = new StringBuilder()

      val prod_code = x.getAs[String]("prod_code")
      val prod_name = x.getAs[String]("prod_name")
      val cpai_acc = x.getAs[String]("capi_acc")
      val ecif_cd = x.getAs[String]("ecif_cd")
      val cust_no = x.getAs[String]("cust_no")
      val cust_name = x.getAs[String]("cust_name")
      val prod1_is_buy = x.getAs[String]("prod1_is_buy")
      val prod1_cap_bal_detail=x.getAs[String]("prod1_cap_bal_detail")
      val latest_risk_grad = x.getAs[String]("latest_risk_grad")
      val cust_grad = x.getAs[String]("cust_grad")
      val cust_type = x.getAs[String]("cust_type")

      val mgr_cd = x.getAs[String]("mgr_cd")
      val mgr_dev = x.getAs[String]("mgr_dev")
      val mgr_sign = x.getAs[String]("mgr_sign")
      val brch_org_cd = x.getAs[String]("brch_org_cd")
      val brch_org_cd_dev = x.getAs[String]("brch_org_cd_dev")
      val brch_org_cd_sign = x.getAs[String]("brch_org_cd_sign")
      val brch_org_level_cd = x.getAs[String]("brch_org_level_cd")
      val brch_org_level_cd_dev = x.getAs[String]("brch_org_level_cd_dev")
      val brch_org_level_cd_sign = x.getAs[String]("brch_org_level_cd_sign")

      val prod1_total_asset =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod1_total_asset"))
      val prod1_total_asset1=prod1_total_asset.bigDecimal

      val prod1_cap_bal =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod1_cap_bal"))
      val prod1_cap_bal1=prod1_cap_bal.bigDecimal

      val prod1_dz_asset =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod1_dz_asset"))
      val prod1_dz_asset1=prod1_dz_asset.bigDecimal

      val prod1_hb_asset = scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod1_hb_asset"))
      val prod1_hb_asset1=prod1_hb_asset.bigDecimal

      val prod1_fund_asset_rate =scala.math.BigDecimal(x.getAs[java.math.BigDecimal]("prod1_fund_asset_rate"))
      val prod1_fund_asset_rate1=prod1_fund_asset_rate.bigDecimal

      val prod1_pos_hb_flag = x.getAs[String]("prod1_pos_hb_flag")
      val prod1_pos_dz_flag = x.getAs[String]("prod1_pos_dz_flag")
      val prod1_pos_bzj_flag = x.getAs[String]("prod1_pos_bzj_flag")
      val prod1_dj_flag = x.getAs[String]("prod1_dj_flag")
      val prod1_pos_flag = x.getAs[String]("prod1_pos_flag")
      val prod1_buy_flag = x.getAs[String]("prod1_buy_flag")
      val prod1_buy_gp = x.getAs[String]("prod1_buy_gp")



      val _id = new ID()
      val index = new Index()
      val companyInfo = new ChgeProdSaleRpt()

      _id.set_id(UUID.randomUUID().toString)
      index.setIndex(_id)
      companyInfo.setProdCode(prod_code)
      companyInfo.setProdName(prod_name)
      companyInfo.setCapiAcc(cpai_acc)
      companyInfo.setEcifCd(ecif_cd)
      companyInfo.setCustNo(cust_no)
      companyInfo.setCustName(cust_name)
      companyInfo.setProd1IsBuy(prod1_is_buy)
      companyInfo.setMgrCd(mgr_cd)
      companyInfo.setBrchOrgCd(brch_org_cd)
      companyInfo.setBrchOrgLevelCd(brch_org_level_cd)
      companyInfo.setProd1CapBalDetail(prod1_cap_bal_detail)
      companyInfo.setMgrSign(mgr_sign)
      companyInfo.setMgrDev(mgr_dev)
      companyInfo.setBrchOrgCdDev(brch_org_cd_dev)
      companyInfo.setBrchOrgCdSign(brch_org_cd_sign)
      companyInfo.setBrchOrgLevelCdDev(brch_org_level_cd_dev)
      companyInfo.setBrchOrgLevelCdSign(brch_org_level_cd_sign)
      companyInfo.setLatestRiskGrad(latest_risk_grad)
      companyInfo.setCustGrad(cust_grad)
      companyInfo.setCustType(cust_type)
      companyInfo.setMgrDev(mgr_dev)
      companyInfo.setMgrSign(mgr_sign)
      companyInfo.setProd1TotalAsset(prod1_total_asset1)
      companyInfo.setProd1CapBal(prod1_cap_bal1)
      companyInfo.setProd1DzAsset(prod1_dz_asset1)
      companyInfo.setProd1HbAsset(prod1_hb_asset1)
      companyInfo.setProd1FundAssetRate(prod1_fund_asset_rate1)
      companyInfo.setProd1PosHbFlag(prod1_pos_hb_flag)
      companyInfo.setProd1PosDzFlag(prod1_pos_dz_flag)
      companyInfo.setProd1PosBzjFlag(prod1_pos_bzj_flag)
      companyInfo.setProd1DjFlag(prod1_dj_flag)
      companyInfo.setProd1PosFlag(prod1_pos_flag)
      companyInfo.setProd1BuyFlag(prod1_buy_flag)
      companyInfo.setProd1BuyGp(prod1_buy_gp)

      val mapper = new ObjectMapper()

      val indexJson = mapper.writeValueAsString(index) //企业自增编码json串
      val companyInfoJson = mapper.writeValueAsString(companyInfo) //企业信息串

      cominfo = cominfo.append(indexJson + "\n" + companyInfoJson) //json结果串
      cominfo.toString()
    })
    val path = new Path(outputPath) //创建文本输出路径
    val fs = FileSystem.get(URI.create(outputPath), conf)
    if (fs.exists(path)) {
      fs.delete(path, true)
    }
    result.coalesce(splitNum.toInt, true).saveAsTextFile(outputPath)
    sc.stop()
  }
}
